#ifndef ADD_BORROWDIALOG_H
#define ADD_BORROWDIALOG_H


#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/datectrl.h> // 确保包含日期选择器头文件
#include <wx/choice.h>

class AddBorrowDialog : public wxDialog {
public:
    AddBorrowDialog(wxWindow* parent);

    // 获取用户输入的借阅信息
    BorrowInfo GetBorrowInfo() const;

private:
    void OnOK(wxCommandEvent& event);
    void OnCancel(wxCommandEvent& event);

    wxTextCtrl* m_bookIdCtrl;
    wxTextCtrl* m_userIdCtrl;
    wxDatePickerCtrl* m_borrowDateCtrl;
    wxDatePickerCtrl* m_returnDateCtrl;

    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(AddBorrowDialog, wxDialog)
    EVT_BUTTON(wxID_OK, AddBorrowDialog::OnOK)
    EVT_BUTTON(wxID_CANCEL, AddBorrowDialog::OnCancel)
END_EVENT_TABLE()

AddBorrowDialog::AddBorrowDialog(wxWindow* parent)
    : wxDialog(parent, wxID_ANY, wxT("添加图书借阅信息"), wxDefaultPosition, wxSize(400, 350)) {

    wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);

    wxFlexGridSizer* fgs = new wxFlexGridSizer(2, 5, 9);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("图书ID")), 0, wxALIGN_CENTER_VERTICAL);
    m_bookIdCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_bookIdCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("用户ID")), 0, wxALIGN_CENTER_VERTICAL);
    m_userIdCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_userIdCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("借阅日期")), 0, wxALIGN_CENTER_VERTICAL);
    m_borrowDateCtrl = new wxDatePickerCtrl(this, wxID_ANY, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DROPDOWN | wxDP_SHOWCENTURY);
    fgs->Add(m_borrowDateCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("归还日期")), 0, wxALIGN_CENTER_VERTICAL);
    m_returnDateCtrl = new wxDatePickerCtrl(this, wxID_ANY, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DROPDOWN | wxDP_SHOWCENTURY);
    fgs->Add(m_returnDateCtrl, 1, wxEXPAND);


    vbox->Add(fgs, 1, wxALL | wxEXPAND, 10);

    wxBoxSizer* btnSizer = new wxBoxSizer(wxHORIZONTAL);
    btnSizer->Add(new wxButton(this, wxID_OK, wxT("确定")), 0, wxALL, 5);
    btnSizer->Add(new wxButton(this, wxID_CANCEL, wxT("取消")), 0, wxALL, 5);

    vbox->Add(btnSizer, 0, wxALIGN_CENTER);

    SetSizer(vbox);
    Centre();
}

BorrowInfo AddBorrowDialog::GetBorrowInfo() const {
    // 获取借阅日期和归还日期并格式化为 time_t
    wxDateTime borrowDate = m_borrowDateCtrl->GetValue();
    wxDateTime returnDate = m_returnDateCtrl->GetValue();
    time_t borrowDateTicks = borrowDate.GetTicks();
    time_t returnDateTicks = returnDate.GetTicks();


    return BorrowInfo(
        -1, // BorrowID 设置为 -1 表示自增
        m_bookIdCtrl->GetValue().ToStdString(),
        m_userIdCtrl->GetValue().ToStdString(),
        borrowDateTicks,
        returnDateTicks,
        "已借出"
    );
}

void AddBorrowDialog::OnOK(wxCommandEvent& event) {
    EndModal(wxID_OK);
}

void AddBorrowDialog::OnCancel(wxCommandEvent& event) {
    EndModal(wxID_CANCEL);
}






#endif // ADD_BORROWDIALOG_H